﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IFRS.Web;
using System.Data;
using IFRS.DataAccess.DAL;
using IFRS.Models;
using IFRS_Global;

public partial class IFRS_WIN04_4A : PageBase
{
    #region "---------- Property -----------"

    private readonly SPR_SUBSUDYLOAN dalSUBSUDYLOAN = new SPR_SUBSUDYLOAN();
    private SubsidyLoan subsidyloan = new SubsidyLoan();

    #endregion

    #region "---------- Events -----------"

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!(Page.IsPostBack || Page.IsCallback))
        {
            this.ViewMenu();
            BindControlData();
            SetErrorMessage();
            BindData();
        }
        else
            ClientScript.RegisterHiddenField("isPostBack", "1");
    }

    protected void btnSAVE_NEW_Click(object sender, EventArgs e)
    {
        SaveData(true);
    }

    protected void btnSAVE_Click(object sender, EventArgs e)
    {
        SaveData(false);
    }

    protected void btnCLEAR_Click(object sender, EventArgs e)
    {
        Response.Redirect(ResolveUrl(Request.Url.AbsoluteUri));
    }

    protected void btnCANCEL_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/IFRS/WIN04_1S.aspx");
    }

    #endregion

    #region "---------- Methods -----------"

    private void ViewMenu()
    {
        this.MenuTitleBar = Resources.ifrsResource.Add + Resources.ifrsMenu.WIN04;
    }

    private void SetErrorMessage()
    {
        btnSAVE.ValidationGroup = "Master";
        btnSAVE_NEW.ValidationGroup = "Master";
        reqTRANSACTION_DATE.SetRequiredField(lblTRANSACTION_DATE, txtTRANSACTION_DATE, btnSAVE);
        reqACCOUNT_NO.SetRequiredField(lblACCOUNT_NO, txtACCOUNT_NO, btnSAVE);
        reqCOST_CENTER.SetRequiredField(lblCOST_CENTER, txtCOST_CENTER, btnSAVE);
        reqGL_ACCOUNT.SetRequiredField(lblGL_ACCOUNT, txtGL_ACCOUNT, btnSAVE);
        reqCURRENCY_CODE.SetRequiredField(lblCURRENCY_CODE, txtCURRENCY_CODE, btnSAVE);
        reqREFERENCE_INTEREST_CD.SetRequiredField(lblREFERENCE_INTEREST_CD, txtREFERENCE_INTEREST_NM, btnSAVE);
        reqADDITIONAL_INTEREST.SetRequiredField(lblADDITIONAL_INTEREST, txtADDITIONAL_INTEREST, btnSAVE);
    }

    private void BindControlData()
    {
        btnCOST_CENTER.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/CostCenter.aspx") + "','700','550');");
        btnGL_ACCOUNT.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/GLAccount.aspx") + "','700','550');");
        btnREFERENCE_INTEREST_CD.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/ReferenceInterest.aspx") + "','700','550');");

        txtCOST_CENTER.AddControl(hidCOST_CENTER.ClientID);
        txtCOST_CENTER.AddControl(txtCOST_CENTER.ClientID);
        txtCOST_CENTER.ControlSearch = txtCOST_CENTER.ClientID;
        txtCOST_CENTER.AddQueryString("bIsActive", "1");
        txtCOST_CENTER.SearchKey = "VWCostCenter_oCOST_CENTER_CD_oCOST_CENTER_NM";

        txtGL_ACCOUNT.AddControl(hidGL_ACCOUNT.ClientID);
        txtGL_ACCOUNT.AddControl(txtGL_ACCOUNT.ClientID);
        txtGL_ACCOUNT.ControlSearch = txtGL_ACCOUNT.ClientID;
        txtGL_ACCOUNT.AddQueryString("bIsActive", "1");
        txtGL_ACCOUNT.SearchKey = "GLAccount_oGLAccountCode_oGLAccountName";

        txtCURRENCY_CODE.AddControl(hidCURRENCY_CODE.ClientID);
        txtCURRENCY_CODE.AddControl(txtCURRENCY_CODE.ClientID);
        txtCURRENCY_CODE.ControlSearch = txtCURRENCY_CODE.ClientID;
        txtCURRENCY_CODE.AddQueryString("bIsActive", "1");
        txtCURRENCY_CODE.SearchKey = "Currency_oCurrencyCode_oCurrencyName";
        txtCURRENCY_CODE.Text = DefaultValue.CurrencyCode.THB;
        hidCURRENCY_CODE.Value = DefaultValue.CurrencyCode.THB;

        txtREFERENCE_INTEREST_NM.AddControl(hidREFERENCE_INTEREST_CD.ClientID);
        txtREFERENCE_INTEREST_NM.AddControl(txtREFERENCE_INTEREST_NM.ClientID);
        txtREFERENCE_INTEREST_NM.ControlSearch = txtREFERENCE_INTEREST_NM.ClientID;
        txtREFERENCE_INTEREST_NM.AddQueryString("bIsActive", "1");
        txtREFERENCE_INTEREST_NM.SearchKey = "ReferenceInterest_oReferenceInterestCD_oReferenceInterestNM";

        SetControlReadOnly(txtIS_CONFIRMED);
    }

    private void BindData()
    {
        if (Request.QueryString["id"] == null)
            return;

        int ID = Convert.ToInt32(Request.QueryString["id"].ToString());
        DataTable dtSubsidyLoan = dalSUBSUDYLOAN.SEARCH_BYID(ID);
        List<SubsidyLoan> SubsidyLoanList = new List<SubsidyLoan>();
        ConvertHelper.ConvertDataTableToObject(dtSubsidyLoan, SubsidyLoanList);
        if (dtSubsidyLoan.Rows.Count > 0)
        {
            // hidSubsidyLoanID.Value = SubsidyLoanList[0].SubsidyLoanID.ToString();
            txtTRANSACTION_DATE.Text = DateTime.Now.DateToString();
            txtACCOUNT_NO.Text = SubsidyLoanList[0].AccountNo;
            txtCOST_CENTER.Text = SubsidyLoanList[0].COST_CENTER_NM;
            hidCOST_CENTER.Value = SubsidyLoanList[0].CostCenter;
            txtGL_ACCOUNT.Text = SubsidyLoanList[0].GLAccountName;
            hidGL_ACCOUNT.Value = SubsidyLoanList[0].GLAccount;
            txtCURRENCY_CODE.Text = SubsidyLoanList[0].CurrencyCode;
            txtREFERENCE_INTEREST_NM.Text = SubsidyLoanList[0].ReferenceInterestNM;
            hidREFERENCE_INTEREST_CD.Value = SubsidyLoanList[0].ReferenceInterestCD;
            txtADDITIONAL_INTEREST.Text = SubsidyLoanList[0].AdditionalInterest.ToString();
            chkIS_COMPENSATE.Checked = SubsidyLoanList[0].IsCompensate;
            // txtIS_CONFIRMED.Text = (bool)SubsidyLoanList[0].IsConfirmed ? Resources.ifrsField.CONFIRM : Resources.ifrsField.CREATE;
        }
    }

    private void SaveData(bool SaveNew)
    {
        try
        {
            subsidyloan.TransactionDate = txtTRANSACTION_DATE.Text.StringToDateAllowNull();
            subsidyloan.AccountNo = txtACCOUNT_NO.Text;
            subsidyloan.CostCenter = hidCOST_CENTER.Value;
            subsidyloan.GLAccount = hidGL_ACCOUNT.Value;
            subsidyloan.CurrencyCode = txtCURRENCY_CODE.Text;
            subsidyloan.ReferenceInterestCD = hidREFERENCE_INTEREST_CD.Value;
            subsidyloan.AdditionalInterest = txtADDITIONAL_INTEREST.Text.AsDecimal();
            subsidyloan.IsCompensate = chkIS_COMPENSATE.Checked;
            subsidyloan.IsConfirmed = false;
            subsidyloan.SetStandardField();

            int iID = dalSUBSUDYLOAN.INSERT(subsidyloan);
            if (iID > 0)
            {
                if (SaveNew)
                    this.JS_Alert(Resources.ifrsResource.SaveData, ResolveUrl(Request.Url.AbsolutePath));
                else
                    this.JS_Alert(Resources.ifrsResource.SaveData, ResolveUrl(string.Format("~/IFRS/WIN04_2E.aspx?&id={0}", iID)));
            }
            else
            {
                if (iID == -1)
                    Utility.AlertMessage(Page, Resources.ifrsResource.Duplicate);
                else
                    Utility.AlertMessage(Page, Resources.ifrsResource.CanNotSaveData);
            }

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    #endregion
}